from django.db import models

class Supplier(models.Model):
    name = models.CharField(max_length=255)
    contact = models.CharField(max_length=255)

class Product(models.Model):
    name = models.CharField(max_length=255)
    quantity = models.IntegerField(default=0)

class ReceivingOrder(models.Model):
    supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=50, choices=[('created', 'Created'), ('approved', 'Approved'), ('received', 'Received')])

class ShippingOrder(models.Model):
    destination = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=50, choices=[('created', 'Created'), ('approved', 'Approved'), ('shipped', 'Shipped')])

class InventoryCheck(models.Model):
    task_name = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
    status = models.CharField(max_length=50, choices=[('open', 'Open'), ('completed', 'Completed')])

class InventoryTransfer(models.Model):
    source = models.CharField(max_length=255)
    destination = models.CharField(max_length=255)
    created_at = models.DateTimeField(auto_now_add=True)
status = models.CharField(max_length=50, choices=[('created', 'Created'), ('approved', 'Approved'), ('transferred', 'Transferred')])


from rest_framework import serializers
from .models import ReceivingOrder, ShippingOrder, InventoryCheck, InventoryTransfer

class ReceivingOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReceivingOrder
        fields = '__all__'

class ShippingOrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = ShippingOrder
        fields = '__all__'

class InventoryCheckSerializer(serializers.ModelSerializer):
    class Meta:
        model = InventoryCheck
        fields = '__all__'

class InventoryTransferSerializer(serializers.ModelSerializer):
    class Meta:
        model = InventoryTransfer
        fields = '__all__'

from rest_framework.views import APIView
from rest_framework.response import Response
from .models import ReceivingOrder, ShippingOrder, InventoryCheck, InventoryTransfer
from .serializers import ReceivingOrderSerializer, ShippingOrderSerializer, InventoryCheckSerializer, InventoryTransferSerializer

class ReceivingOrderView(APIView):
    def post(self, request):
        serializer = ReceivingOrderSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({"message": "Receiving order created successfully"})
        return Response(serializer.errors)

    def get(self, request):
        orders = ReceivingOrder.objects.all()
        serializer = ReceivingOrderSerializer(orders, many=True)
        return Response(serializer.data)

class ShippingOrderView(APIView):
    def post(self, request):
        serializer = ShippingOrderSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({"message": "Shipping order created successfully"})
        return Response(serializer.errors)

    def get(self, request):
        orders = ShippingOrder.objects.all()
        serializer = ShippingOrderSerializer(orders, many=True)
        return Response(serializer.data)

class InventoryCheckView(APIView):
    def post(self, request):
        serializer = InventoryCheckSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({"message": "Inventory check created successfully"})
        return Response(serializer.errors)

    def get(self, request):
        tasks = InventoryCheck.objects.all()
        serializer = InventoryCheckSerializer(tasks, many=True)
        return Response(serializer.data)

class InventoryTransferView(APIView):
    def post(self, request):
        serializer = InventoryTransferSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response({"message": "Inventory transfer created successfully"})
        return Response(serializer.errors)

    def get(self, request):
        transfers = InventoryTransfer.objects.all()
        serializer = InventoryTransferSerializer(transfers, many=True)
        return Response(serializer.data)

from django.urls import path
from .views import ReceivingOrderView, ShippingOrderView, InventoryCheckView, InventoryTransferView

urlpatterns = [
    path('receiving/', ReceivingOrderView.as_view(), name='receiving'),
    path('shipping/', ShippingOrderView.as_view(), name='shipping'),
    path('inventory-check/', InventoryCheckView.as_view(), name='inventory-check'),
    path('inventory-transfer/', InventoryTransferView.as_view(), name='inventory-transfer'),
]

<!DOCTYPE html>
<html>
<head>
    <title>Receiving Orders</title>
</head>
<body>
    <h1>Receiving Orders</h1>
    <button onclick="createOrder()">Create Receiving Order</button>
    <div id="orders"></div>
    <script>
        async function fetchOrders() {
            const response = await fetch('/api/receiving/');
            const data = await response.json();
            document.getElementById('orders').innerText = JSON.stringify(data);
        }

        async function createOrder() {
            const response = await fetch('/api/receiving/', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ supplier: 1, status: 'created' })
            });
            const data = await response.json();
            alert(data.message);
            fetchOrders();
        }

        fetchOrders();
    </script>
</body>
</html>

from django.urls import path
from .views import ReceivingOrderView, ShippingOrderView, InventoryCheckView, InventoryTransferView

urlpatterns = [
    path('receiving/', ReceivingOrderView.as_view(), name='receiving'),
    path('shipping/', ShippingOrderView.as_view(), name='shipping'),
    path('inventory-check/', InventoryCheckView.as_view(), name='inventory-check'),
    path('inventory-transfer/', InventoryTransferView.as_view(), name='inventory-transfer'),
]

CREATE TABLE ReceivingOrder (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    supplier_id INTEGER,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    status TEXT
);

CREATE TABLE ShippingOrder (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    destination TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    status TEXT
);

CREATE TABLE InventoryCheck (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    task_name TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    status TEXT
);

CREATE TABLE InventoryTransfer (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    source TEXT,
    destination TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    status TEXT
);